<?php
declare (strict_types = 1);

namespace app\admin\controller\erp;

use app\admin\controller\AuthController;
use app\admin\model\erp\ErpStorage;
use app\admin\model\system\SystemMenus;
use bbweb\services\UtilService as Util;
use think\Request;

class BasicsStorage extends AuthController
{
    /**
     * 显示列表
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function index(Request $req)
    {
        if ($req->isAjax()) {
            $data = [];
            $page = $req->param('page/d', 1);
            $limit = $req->param('limit/d', 15);
            $query = ErpStorage::order('id DESC');
            // 条件搜索
            $name = $req->param('name');
            if (!is_null($name) && strlen($name)) {
                $query->where('name','like',"%$name%");
            }
            $data['count'] = $query->count('id');
            $data['data'] = $query->page($page,$limit)->select()->toArray();
            $data['code'] = 0;
            $data['msg'] = '';

            return json($data);
        }
        $sonAuth = SystemMenus::getSonAuth($req->controller(), $req->action(),array_keys($this->auth));

        // 显示页面
        $this->assign('sonAuth', $sonAuth);
        return $this->fetch();
    }

    /**
     * 创建资源.
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function add(Request $req)
    {
        if($req->isPost()) {
            $data = Util::postMore([
                ['name',''],
                ['username',''],
                ['phone',''],
                ['address',''],
            ], $req);
            if (!$data['name']) $this->error('请输入仓库名称');

            $data['create_at'] = time();
            $data['update_at'] = time();
            $res = ErpStorage::create($data);
            if($res) {
                $this->success('操作成功!');
            } else {
                $this->error('操作失败');
            }
        }
        return $this->fetch();
    }

    /**
     * 更新资源
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function edit(Request $req)
    {
        $id = intval($req->param('id/d'));
        if(!$id) $this->error('参数错误','', -1);
        $storage = ErpStorage::where('id', $id)->find();
        if(!$storage) $this->error('参数错误','', -1);
        if($req->isPost()) {
            $data = [];
            if($req->param('change/d')) {
                $data['status'] = $req->param('status/d');
            } else {
                $data = Util::postMore([
                    ['name', ''],
                    ['username', ''],
                    ['phone', ''],
                    ['address', ''],
                ], $req);
                if (!$data['name']) $this->error('请输入仓库名称');
            }

            $data['update_at'] = time();
            $res = ErpStorage::where('id', $id)->update($data);
            if($res) {
                $this->success('操作成功!');
            } else {
                $this->error('操作失败');
            }
        }
        $this->assign('storage', $storage);
        return $this->fetch();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
